package com.PVZ.utils;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import java.util.concurrent.*;

/**
 * Created by @Author: kabuda on @Date: 2021/5/30 10:44
 *
 * 配置线程池
 */
public class MyExecutors {
    private static final int THREAD_POOL_SIZE = 10;

    public static Executor createCacheExecutor(String name) {
        ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat(name + "-%d").build();
// 创建线程池，其中任务队列需要结合实际情况设置合理的容量
        ThreadPoolExecutor executor = new ThreadPoolExecutor(THREAD_POOL_SIZE,
                THREAD_POOL_SIZE,
                40L,
                TimeUnit.SECONDS,
                new SynchronousQueue<>(),
                threadFactory,
                new ThreadPoolExecutor.AbortPolicy());

        return executor;
    }

    public static ScheduledExecutorService createScheduleExecutor(String name){
        ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat(name + "-%d").build();

        ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor(threadFactory);

        return  scheduledExecutor;
    }
}
